home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / fscompr / gzio.c < prev    next >
C/C++ Source or Header  |  1997-12-23  |  16KB  |  524 lines

  1. /* gzio.c -- IO on .gz files
  2.  * Copyright (C) 1995-1996 Jean-loup Gailly.
  3.  * For conditions of distribution and use, see copyright notice in zlib.h
  4.  */
  5.  
  6. /* $Id: gzio.c,v 1.14 1996/07/24 13:41:01 me Exp $ */
  7.  
  8. #include <stdio.h>
  9.  
  10. #include "zutil.h"
  11.  
  12. struct internal_state {int dummy;}; /* for buggy compilers */
  13.  
  14. #define Z_BUFSIZE 4096
  15.  
  16. #define ALLOC(size) malloc(size)
  17. #define TRYFREE(p) {if (p) free(p);}
  18.  
  19. static int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
  20.  
  21. /* gzip flag byte */
  22. #define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
  23. #define HEAD_CRC     0x02 /* bit 1 set: header CRC present */
  24. #define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
  25. #define ORIG_NAME    0x08 /* bit 3 set: original file name present */
  26. #define COMMENT      0x10 /* bit 4 set: file comment present */
  27. #define RESERVED     0xE0 /* bits 5..7: reserved */
  28.  
  29. typedef struct gz_stream {
  30.     z_stream stream;
  31.     int      z_err;   /* error code for last stream operation */
  32.     int      z_eof;   /* set if end of input file */
  33.     FILE     *file;   /* .gz file */
  34.     Byte     *inbuf;  /* input buffer */
  35.     Byte     *outbuf; /* output buffer */
  36.     uLong    crc;     /* crc32 of uncompressed data */
  37.     char     *msg;    /* error message */
  38.     char     *path;   /* path name for debugging only */
  39.     int      transparent; /* 1 if input file is not a .gz file */
  40.     char     mode;    /* 'w' or 'r' */
  41. } gz_stream;
  42.  
  43.  
  44. local gzFile gz_open      OF((const char *path, const char *mode, int  fd));
  45. local int    get_byte     OF((gz_stream *s));
  46. local void   check_header OF((gz_stream *s));
  47. local int    destroy      OF((gz_stream *s));
  48. local void   putLong      OF((FILE *file, uLong x));
  49. local uLong  getLong      OF((gz_stream *s));
  50.  
  51. /* ===========================================================================
  52.      Opens a gzip (.gz) file for reading or writing. The mode parameter
  53.    is as in fopen ("rb" or "wb"). The file is given either by file descriptor
  54.    or path name (if fd == -1).
  55.      gz_open return NULL if the file could not be opened or if there was
  56.    insufficient memory to allocate the (de)compression state; errno
  57.    can be checked to distinguish the two cases (if errno is zero, the
  58.    zlib error is Z_MEM_ERROR).
  59. */
  60. local gzFile gz_open (path, mode, fd)
  61.     const char *path;
  62.     const char *mode;
  63.     int  fd;
  64. {
  65.     int err;
  66.     int level = Z_DEFAULT_COMPRESSION; /* compression level */
  67.     char *p = (char*)mode;
  68.     gz_stream *s;
  69.     char fmode[80]; /* copy of mode, without the compression level */
  70.     char *m = fmode;
  71.  
  72.     if (!path || !mode) return Z_NULL;
  73.  
  74.     s = (gz_stream *)ALLOC(sizeof(gz_stream));
  75.     if (!s) return Z_NULL;
  76.  
  77.     s->stream.zalloc = (alloc_func)0;
  78.     s->stream.zfree = (free_func)0;
  79.     s->stream.opaque = (voidpf)0;
  80.     s->stream.next_in = s->inbuf = Z_NULL;
  81.     s->stream.next_out = s->outbuf = Z_NULL;
  82.     s->stream.avail_in = s->stream.avail_out = 0;
  83.     s->file = NULL;
  84.     s->z_err = Z_OK;
  85.     s->z_eof = 0;
  86.     s->crc = crc32(0L, Z_NULL, 0);
  87.     s->msg = NULL;
  88.     s->transparent = 0;
  89.  
  90.     s->path = (char*)ALLOC(strlen(path)+1);
  91.     if (s->path == NULL) {
  92.         return destroy(s), (gzFile)Z_NULL;
  93.     }
  94.     strcpy(s->path, path); /* do this early for debugging */
  95.  
  96.     s->mode = '\0';
  97.     do {
  98.         if (*p == 'r') s->mode = 'r';
  99.         if (*p == 'w' || *p == 'a') s->mode = 'w';
  100.         if (*p >= '0' && *p <= '9') {
  101.         level = *p - '0';
  102.     } else {
  103.         *m++ = *p; /* copy the mode */
  104.     }
  105.     } while (*p++ && m != fmode + sizeof(fmode));
  106.     if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL;
  107.     
  108.     if (s->mode == 'w') {
  109.         err = deflateInit2(&(s->stream), level,
  110.                            Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, 0);
  111.         /* windowBits is passed < 0 to suppress zlib header */
  112.  
  113.         s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
  114.  
  115.         if (err != Z_OK || s->outbuf == Z_NULL) {
  116.             return destroy(s), (gzFile)Z_NULL;
  117.         }
  118.     } else {
  119.         err = inflateInit2(&(s->stream), -MAX_WBITS);
  120.         s->stream.next_in  = s->inbuf = (Byte*)ALLOC(Z_BUFSIZE);
  121.  
  122.         if (err != Z_OK || s->inbuf == Z_NULL) {
  123.             return destroy(s), (gzFile)Z_NULL;
  124.         }
  125.     }
  126.     s->stream.avail_out = Z_BUFSIZE;
  127.  
  128.     errno = 0;
  129.     s->file = fd < 0 ? FOPEN(path, fmode) : (FILE*)fdopen(fd, fmode);
  130.  
  131.     if (s->file == NULL) {
  132.         return destroy(s), (gzFile)Z_NULL;
  133.     }
  134.     if (s->mode == 'w') {
  135.         /* Write a very simple .gz header:
  136.          */
  137.         fprintf(s->file, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1],
  138.              Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE);
  139.     } else {
  140.     check_header(s); /* skip the .gz header */
  141.     }
  142.     return (gzFile)s;
  143. }
  144.  
  145. /* ===========================================================================
  146.      Opens a gzip (.gz) file for reading or writing.
  147. */
  148. gzFile EXPORT gzopen (path, mode)
  149.     const char *path;
  150.     const char *mode;
  151. {
  152.     return gz_open (path, mode, -1);
  153. }
  154.  
  155. /* ===========================================================================
  156.      Associate a gzFile with the file descriptor fd. fd is not dup'ed here
  157.    to mimic the behavio(u)r of fdopen.
  158. */
  159. gzFile EXPORT gzdopen (fd, mode)
  160.     int fd;
  161.     const char *mode;
  162. {
  163.     char name[20];
  164.  
  165.     if (fd < 0) return (gzFile)Z_NULL;
  166.     sprintf(name, "<fd:%d>", fd); /* for debugging */
  167.  
  168.     return gz_open (name, mode, fd);
  169. }
  170.  
  171. /* ===========================================================================
  172.      Read a byte from a gz_stream; update next_in and avail_in. Return EOF
  173.    for end of file.
  174.    IN assertion: the stream s has been sucessfully opened for reading.
  175. */
  176. local int get_byte(s)
  177.     gz_stream *s;
  178. {
  179.     if (s->z_eof) return EOF;
  180.     if (s->stream.avail_in == 0) {
  181.     errno = 0;
  182.     s->stream.avail_in = fread(s->inbuf, 1, Z_BUFSIZE, s->file);
  183.     if (s->stream.avail_in == 0) {
  184.         s->z_eof = 1;
  185.         if (ferror(s->file)) s->z_err = Z_ERRNO;
  186.         return EOF;
  187.     }
  188.     s->stream.next_in = s->inbuf;
  189.     }
  190.     s->stream.avail_in--;
  191.     return *(s->stream.next_in)++;
  192. }
  193.  
  194. /* ===========================================================================
  195.       Check the gzip header of a gz_stream opened for reading. Set the stream
  196.     mode to transparent if the gzip magic header is not present; set s->err
  197.     to Z_DATA_ERROR if the magic header is present but the rest of the header
  198.     is incorrect.
  199.     IN assertion: the stream s has already been created sucessfully;
  200.        s->stream.avail_in is zero for the first time, but may be non-zero
  201.        for concatenated .gz files.
  202. */
  203. local void check_header(s)
  204.     gz_stream *s;
  205. {
  206.     int method; /* method byte */
  207.     int flags;  /* flags byte */
  208.     uInt len;
  209.     int c;
  210.  
  211.     /* Check the gzip magic header */
  212.     for (len = 0; len < 2; len++) {
  213.     c = get_byte(s);
  214.     if (c != gz_magic[len]) {
  215.         s->transparent = 1;
  216.         if (c != EOF) s->stream.avail_in++, s->stream.next_in--;
  217.         s->z_err = s->stream.avail_in != 0 ? Z_OK : Z_STREAM_END;
  218.         return;
  219.     }
  220.     }
  221.     method = get_byte(s);
  222.     flags = get_byte(s);
  223.     if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
  224.     s->z_err = Z_DATA_ERROR;
  225.     return;
  226.     }
  227.  
  228.     /* Discard time, xflags and OS code: */
  229.     for (len = 0; len < 6; len++) (void)get_byte(s);
  230.  
  231.     if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
  232.     len  =  (uInt)get_byte(s);
  233.     len += ((uInt)get_byte(s))<<8;
  234.     /* len is garbage if EOF but the loop below will quit anyway */
  235.     while (len-- != 0 && get_byte(s) != EOF) ;
  236.     }
  237.     if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
  238.     while ((c = get_byte(s)) != 0 && c != EOF) ;
  239.     }
  240.     if ((flags & COMMENT) != 0) {   /* skip the .gz file comment */
  241.     while ((c = get_byte(s)) != 0 && c != EOF) ;
  242.     }
  243.     if ((flags & HEAD_CRC) != 0) {  /* skip the header crc */
  244.     for (len = 0; len < 2; len++) (void)get_byte(s);
  245.     }
  246.     s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
  247. }
  248.  
  249.  /* ==========================================